﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace ViewComponents
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ViewComponents"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ViewComponents;assembly=ViewComponents"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:CustomControl1/>
    ///
    /// </summary>
    [TemplatePart(Name = "PART_ContentHost", Type = typeof(ScrollViewer))]
    [TemplatePart(Name = "GetWorkbookButton", Type = typeof(Button))]
    public class WorkbookBox : TextBox
    {
        ScrollViewer mContent;
        Button mGetWorkbookButton;
        FileInfo mFile;
        bool mAlreadySelected = false;

        static WorkbookBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WorkbookBox), new FrameworkPropertyMetadata(typeof(WorkbookBox)));
        }

        public FileInfo File
        {
            get { return mFile; }
            set
            {
                mFile = value;
                Text = value.Name;
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            mContent = (ScrollViewer)Template.FindName("PART_ContentHost", this);
            mContent.GotFocus += Content_GotFocus;
            mContent.GotMouseCapture += Content_GotMouseCapture;
            mContent.LostFocus += Content_LostFocus;

            mGetWorkbookButton = (Button)Template.FindName("GetWorkbookButton", this);
            mGetWorkbookButton.Click += Button_Click;
        }

        void  Content_GotFocus(object sender, RoutedEventArgs e)
        {
 	        if (File != null)
                Text = File.FullName;
        }

        void Content_GotMouseCapture(object sender, MouseEventArgs e)
        {
 	        if (!mAlreadySelected)
            {
                SelectAll();
                mAlreadySelected = true;
            }
        }

        void  Content_LostFocus(object sender, RoutedEventArgs e)
        {
 	        if (Text != string.Empty)
            {
                if (File == null || File.FullName != Text)
                    File = new FileInfo(Text);
                else
                    Text = File.Name;
            }

            mAlreadySelected = false;
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            Excel.Application app = ObjectModel.Excel.Globals.Application;

            dynamic result = app.GetOpenFilename();
            if (result is string)
                File = new FileInfo((string)result);
        }
    }
}
